package com.ald.crossex.service.wechat;

import com.ald.crossex.dao.CeCoreUserDao;
import com.ald.crossex.dao.CeCoreWechatDao;
import com.ald.crossex.dao.CeFeedbookDao;
import com.ald.crossex.dto.wechat.SubmitFeedBackRequest;
import com.ald.crossex.dto.wechat.WechatBaseUserInfoResponse;
import com.ald.crossex.dto.wechat.WechatWebUserInfoResponse;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.entity.TCeCoreWechat;
import com.ald.crossex.entity.TCeFeedbook;
import com.ald.crossex.util.QueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by lishijiong on 15/3/19.
 */
@Service
public class WechatService {

    @Autowired
    private CeCoreWechatDao wechatDao;
    @Autowired
    private CeCoreUserDao userDao;
    @Autowired
    private CeFeedbookDao feedbookDao;
    @Autowired
    private HibernateTemplate hibernateTemplate;


    /**
     * 根据openid 查找是否已经获取过微信用户信息
     *
     * @param openId
     * @return
     */
    public boolean checkWechatExist(String openId) {

        if (getWechatByOpenId(openId).size() > 0) {
            return true;
        }

        return false;
    }

    /**
     * 根据openid 查找微信用户信息
     *
     * @param openId
     * @return
     */
    public List<TCeCoreWechat> getWechatByOpenId(String openId) {

        List<TCeCoreWechat> list = hibernateTemplate.find("from TCeCoreWechat w where w.wid in (select u.wid from TCeCoreUser u , TCeCoreWechat w1 where  u.wid = w1.wid and u.isDelete = 0 and u.type = 1 and w1.openId = ? )", new Object[]{openId});


        if (list != null && list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    /**
     * 添加微信用户
     *
     * @param appId
     * @param lang
     * @param response
     */
    @Transactional
    public TCeCoreWechat addWechat(String appId, String lang, WechatWebUserInfoResponse response) {

        TCeCoreWechat wechat = new TCeCoreWechat();
        wechat.setAppId(appId);
        wechat.setOpenId(response.getOpenid());
        wechat.setCity(response.getCity());
        wechat.setCountry(response.getCountry());
        wechat.setProvince(response.getProvince());
        wechat.setLanguage(lang);
        wechat.setNickName(response.getNickname());
        wechat.setSex(response.getSex());
        wechat.setUnionId(response.getUnionid());
        wechat.setType(1);//网页授权
        wechatDao.create(wechat);
        //添加用户
        TCeCoreUser user = new TCeCoreUser();
        user.setWid(wechat.getWid());
        user.setNickname(wechat.getNickName());
        user.setUsername(wechat.getNickName());
        user.setPassword("123456");
        user.setType(1);//微信用户
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setIsDelete(0);

        userDao.create(user);
        return wechat;
    }

    /**
     * @param appId
     * @param response
     */
    @Transactional
    public void subScribeWechat(String appId, WechatBaseUserInfoResponse response) {
        List<TCeCoreWechat> wechats = getWechatByOpenId(response.getOpenid());
        TCeCoreWechat wechat;
        if (wechats.size() > 0) {//重新关注，更新数据
            wechat = wechats.get(0);
            wechat.setAppId(appId);
            wechat.setOpenId(response.getOpenid());
            wechat.setCity(response.getCity());
            wechat.setCountry(response.getCountry());
            wechat.setProvince(response.getProvince());
            wechat.setLanguage(response.getLanguage());
            wechat.setNickName(response.getNickname());
            wechat.setSex(response.getSex());
            wechat.setUnionId(response.getUnionid());
            wechat.setSubScribe(response.getSubscribe());
            wechat.setSubscribeTime(response.getSubscribe_time());
            wechat.setType(0);//公众号关注
            wechatDao.update(wechat);
            userDao.update("update ce_core_user set is_delete = 1 where wid = ?", new Object[]{wechat.getWid()}, QueryFactory.SQL);

        } else {
            //第一次关注，新增数据
            wechat = new TCeCoreWechat();
            wechat.setAppId(appId);
            wechat.setOpenId(response.getOpenid());
            wechat.setCity(response.getCity());
            wechat.setCountry(response.getCountry());
            wechat.setProvince(response.getProvince());
            wechat.setLanguage(response.getLanguage());
            wechat.setNickName(response.getNickname());
            wechat.setSex(response.getSex());
            wechat.setUnionId(response.getUnionid());
            wechat.setSubScribe(response.getSubscribe());
            wechat.setSubscribeTime(response.getSubscribe_time());
            wechat.setType(0);//公众号关注
            wechatDao.create(wechat);
            //添加用户
            TCeCoreUser user = new TCeCoreUser();
            user.setWid(wechat.getWid());
            user.setNickname(wechat.getNickName());
            user.setUsername(wechat.getNickName());
            user.setPassword("123456");
            user.setType(1);//微信用户
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setIsDelete(0);
            userDao.create(user);
        }


    }

    /**
     * 用户取消关注
     *
     * @param appId
     * @param openId
     */
    @Transactional
    public void unSubScribeWechat(String appId, String openId) {

        List<TCeCoreWechat> wechats = getWechatByOpenId(openId);
        if (wechats.size() > 0) {
            TCeCoreWechat wechat = wechats.get(0);
            wechat.setSubScribe(0);

            wechatDao.update(wechat);

            userDao.update("update ce_core_user set is_delete = 1 where wid = ?", new Object[]{wechat.getWid()}, QueryFactory.SQL);

        }


    }

    /**
     * 添加反馈
     *
     * @param request
     */
    @Transactional
    public void submitFeedback(SubmitFeedBackRequest request) {
        TCeFeedbook fb = new TCeFeedbook();
        fb.setWid(request.getWid());
        fb.setTitle(request.getTitle());
        fb.setMessage(request.getMessage());
        fb.setUid(request.getUid());
        fb.setPhone(request.getPhone());
        fb.setEmail(request.getEmail());
        fb.setCreateTime(new Date());
        fb.setIsDelete(0);
        fb.setIsRead(0);
        feedbookDao.create(fb);
    }

}
